package dk.mirasola.systemtraining.bridgewidgets.shared.model.distributionfiltertree;


import dk.mirasola.systemtraining.bridgewidgets.shared.model.Distribution;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Hand;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Seat;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Suit;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.distributionfiltertree.shape.ShapeType;

public final class NTDistributionFilter extends DistributionFilterGroupItem {
    private HcpInterval hcpInterval = new HcpInterval();
    private Seat seat = Seat.NORTH;
    private Boolean fiveCardMajorsAllowed = Boolean.FALSE;
    private Boolean sixCardMinorsAllowed = Boolean.FALSE;

    public HcpInterval getHcpInterval() {
        return hcpInterval;
    }

    public void setHcpInterval(HcpInterval hcpInterval) {
        if (hcpInterval == null) {
            throw new NullPointerException();
        }
        this.hcpInterval = hcpInterval;
    }

    public Seat getSeat() {
        return seat;
    }

    public void setSeat(Seat seat) {
        this.seat = seat;
    }

    public Boolean getFiveCardMajorsAllowed() {
        return fiveCardMajorsAllowed;
    }

    public void setFiveCardMajorsAllowed(Boolean fiveCardMajorsAllowed) {
        this.fiveCardMajorsAllowed = fiveCardMajorsAllowed;
    }

    public Boolean getSixCardMinorsAllowed() {
        return sixCardMinorsAllowed;
    }

    public void setSixCardMinorsAllowed(Boolean sixCardMinorsAllowed) {
        this.sixCardMinorsAllowed = sixCardMinorsAllowed;
    }

    protected boolean matchesInternal(Distribution distribution) {
        Hand hand = distribution.getHand(seat);

        // check HCP i interval
        if (!hcpInterval.contains(hand.getHcp())) {
            return false;
        }

        // hcp is in interval, so check shape is ok
        if (ShapeType.SHAPE_TYPE_4333.matches(hand)) {
            return true;
        }

        if (ShapeType.SHAPE_TYPE_4432.matches(hand)) {
            return true;
        }

        if (ShapeType.SHAPE_TYPE_5332.matches(hand)) {
            // its a 5-3-3-2 hand
            if (fiveCardMajorsAllowed) {
                // five card major is allowed
                return true;
            }
            if (hand.getCardsCount(Suit.DIAMOND) == 5 || hand.getCardsCount(Suit.CLUB) == 5) {
                // five card is in minor
                return true;
            }
        }

        if (sixCardMinorsAllowed && ShapeType.SHAPE_TYPE_6322.matches(hand)) {
            // its a 6322 hand so check that 6 cards are in minor
            if (hand.getCardsCount(Suit.DIAMOND) == 6 || hand.getCardsCount(Suit.CLUB) == 6) {
                // five card is in minor
                return true;
            }
        }

        return false;
    }

    @Override
    public boolean isCopy(GroupItem groupItem) {
        if (!super.isCopy(groupItem)) return false;
        if (!(groupItem instanceof NTDistributionFilter)) return false;

        NTDistributionFilter that = (NTDistributionFilter) groupItem;
        return fiveCardMajorsAllowed == that.fiveCardMajorsAllowed && sixCardMinorsAllowed == that.sixCardMinorsAllowed &&
                seat == that.seat && hcpInterval.equals(that.hcpInterval);
    }

    @Override
    public NTDistributionFilter copy() {
        NTDistributionFilter copy = new NTDistributionFilter();
        copy.setInverted(this.getInverted());
        copy.seat = this.seat;
        copy.hcpInterval = this.hcpInterval.copy();
        copy.fiveCardMajorsAllowed = this.fiveCardMajorsAllowed;
        copy.sixCardMinorsAllowed = this.sixCardMinorsAllowed;
        return copy;
    }
}
